package use;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Customer { //Customer为顾客
    public void cusLogin() throws IOException {
        Login loginer = new Login();
        loginer.cusLogin();
    }

    public void chooseSong() throws IOException {
        System.out.println("请输入点歌方式:" + "\n" + "1、歌名点歌" + " " + "2、明星曲目" + " " + "3、退出系统");
        Scanner sc = new Scanner(System.in);
        String choose = sc.next();
        switch (choose) {
            case "1":
                inputSong();
                return;
            case "2":
                inputStar();
                return;
            case "3":
                viewList();
                return;
            default:
                System.out.println("功能号不存在！请正确输入功能号！");
                chooseSong();
                break;
        }
    }

    public void inputSong() throws IOException {
        System.out.println("请输入歌曲名（若输入N则返回点歌方式选择界面）");
        Scanner sc = new Scanner(System.in);
        FileReader fr = new FileReader("information\\songlibrary.txt");
        BufferedReader bfr = new BufferedReader(fr);
        FileWriter fw = new FileWriter("information\\playlist.txt", true);
        int flag = 0;
        while (flag == 0) {
            String songName = sc.next();
            String si;
            if (!songName.equals("N")) {
                while ((si = bfr.readLine()) != null) {
                    String[] sn = si.split(",");
                    if (songName.equals(sn[0])) {
                        fw.write(si);
                        fw.write("\n");
                        fw.close();
                        inputSong();
                        return;
                    }
                }
                System.out.println("歌单里没有该曲目,请重新输入");
                inputSong();
            } else if (songName.equals("N")) {
                chooseSong();
                return;
            }
        }
    }

    public void inputStar() throws IOException {
        Scanner sc = new Scanner(System.in);


        String star;
        boolean fa = true;

        String starSong="y";
        while (fa) {
            FileReader fileReader = new FileReader("information\\star.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            FileWriter fileWriter = new FileWriter("information\\playlist.txt", true);
            System.out.println("请输入歌手的名字(退出请按N)");
            String starName = sc.next();
            if(starName.equals("N")){
                break;
            }

            while ((star = bufferedReader.readLine())!= null) {
                String[] line = star.split(",");
                if (starName.equals(line[0])) {
                    int i;
                    boolean flag = true;
                    while (flag) {
                        System.out.println("请输入该歌手的歌曲名(若输入N则返回点歌方式选择界面)");
                        starSong = sc.next();
                        for (i = 2; i < line.length; i++) {
                            if (starSong.equals(line[i]) && !starSong.equals("N")) {
                                fileWriter.write(line[i]);
                                fileWriter.write("\r\n");
                                break;
                            } else if (starSong.equals("N")) {
                                flag = false;
                                break;
                            }
                        }
                        if (i == line.length) {
                            System.out.println("该歌曲不存在");
                        }
                    }
                }
            }
            if (star==null && !starSong.equals("N")){
                System.out.println("对不起，该歌手的歌曲尚未录入！");
                System.out.println("请重新输入歌手的姓名");
                inputStar();
            }
            fileWriter.close();
        }
//            System.out.println("1、继续选择其他歌手的曲目 2、退出选歌系统");
//            String sr = sc.next();
//            switch (sr) {
//                case "1":
//                    inputStar();
//                    break;
//                case "2":
//                    System.out.println("退出选歌系统");
                    viewList();
//                    break;
//                default:
//                    System.out.println("功能号不存在！请正确输入功能号！");
//                    break;
//            }
        }


    public void viewList() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("您已退出选歌系统，请选择:");
        System.out.println("1、查看歌单 2、删除歌单上的歌曲 3、退出歌单");
        String st = sc.next();
        switch (st) {
            case "1":
                look();
            case "2":
                look();
                FileReader fr = new FileReader("information\\playlist.txt");
                BufferedReader bfr = new BufferedReader(fr);
                List<String> list2 = new ArrayList<>();
                String str;
                System.out.println("请输入要删除歌曲的歌名");
                String name = sc.next();
                while ((str = bfr.readLine()) != null) {
                    list2.add(str);
                    String[] data = str.split(",");
                    if (name.equals(data[0])) {
                        list2.remove(str);
                    }
                    FileWriter fw = new FileWriter("information\\playlist.txt");
                    for (String sn : list2) {
                        fw.write(sn);
                        fw.write("\r\n");
                    }
                    fw.close();
                }
            case "3":
                System.out.println("您已退出歌单");
                return;
            default:
                System.out.println("功能号不存在！请正确输入功能号！");
                break;
        }
    }

    public void look() throws IOException {
        FileReader fr1 = new FileReader("information\\playlist.txt");
        BufferedReader bfr1 = new BufferedReader(fr1);
        List<String> list1 = new LinkedList<>();
        String si;
        while ((si = bfr1.readLine()) != null) {
            list1.add(si);
        }
        for (String sg : list1) {
            System.out.println(sg);
        }
    }
}


